
# https://help.aliyun.com/zh/model-studio/what-is-model-studio#6b799c8142ohs

import os
from openai import OpenAI
import asyncio
import platform

client = OpenAI(
    api_key = "sk-1341cd7d8e38416ebc0a0e698a622896", #os.getenv("DASHCOPE_API_KEY")
    base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1",
)

# 异步任务列表
async def task(question):
    print("发送问题")

    try:
        completion = client.chat.completions.create(
            model = "qwen-plus",
            messages = [
                {'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': question}
            ]
        )

        print(f"模型回复：{completion.choices[0].message.content}")
    except Exception as e:
        print(f"错误信息：{e}")
        print("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code")


async def main():
    questions = ["你是谁？", "你有什么能力？", "今天天气怎么样？"]
    tasks = [task(q) for q in questions]
    await asyncio.gather(*tasks)

if __name__ == "__main__":
    # 设置事件循环路径
    if platform.system() == 'Windows':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    # 运行主协程
    asyncio.run(main(), debug=False)
    